package com.amsp.oa.oademo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.amsp.oa.oademo.entity.FileControl;
import com.amsp.oa.oademo.mapper.FileControlMapper;
import com.amsp.oa.oademo.service.FileControlService;
import com.amsp.oa.oademo.vo.FileControlVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class FileControlServiceImpl extends ServiceImpl<FileControlMapper, FileControl> implements FileControlService {

    @Override
    public IPage<FileControl> getPage(Integer current, Integer size, String fileNumber) {
        Page<FileControl> page = new Page<>(current, size);
        LambdaQueryWrapper<FileControl> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(fileNumber)) {
            wrapper.like(FileControl::getFileNumber, fileNumber);
        }
        wrapper.orderByDesc(FileControl::getCreateTime);
        return page(page, wrapper);
    }

    @Override
    public FileControlVO getDetail(Long id) {
        FileControl fileControl = getById(id);
        if (fileControl == null) {
            return null;
        }

        FileControlVO vo = new FileControlVO();
        vo.setId(fileControl.getId());
        vo.setFileNumber(fileControl.getFileNumber());
        vo.setFileName(fileControl.getFileName());
        vo.setDepartment(fileControl.getDepartment());
        vo.setVersion(fileControl.getVersion());
        vo.setScheduleCopies(fileControl.getScheduleCopies());
        vo.setRemarks(fileControl.getRemarks());

        // 解析文件类别
        HashMap<String, Boolean> categories = new HashMap<>();
        String fileCategory = fileControl.getFileCategory();
        categories.put("managementManual", fileCategory.contains("MANAGEMENT_MANUAL"));
        categories.put("procedure", fileCategory.contains("PROCEDURE"));
        categories.put("specification", fileCategory.contains("SPECIFICATION"));
        categories.put("technicalDocument", fileCategory.contains("TECHNICAL_DOCUMENT"));
        vo.setFileCategories(categories);

        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateBatchTransactional(List<FileControlVO> vo) {
        List<FileControl> poList = new ArrayList<>();
        vo.stream().forEach(e -> {
            FileControl fileControl = new FileControl();
            BeanUtils.copyProperties(e, fileControl);

            StringBuilder category = new StringBuilder();
            e.getFileCategories().forEach((key, value) -> {
                if (Boolean.TRUE.equals(value)) {
                    if (category.length() > 0) {
                        category.append(",");
                    }
                    switch (key) {
                        case "managementManual":
                            category.append("MANAGEMENT_MANUAL");
                            break;
                        case "procedure":
                            category.append("PROCEDURE");
                            break;
                        case "specification":
                            category.append("SPECIFICATION");
                            break;
                        case "technicalDocument":
                            category.append("TECHNICAL_DOCUMENT");
                            break;
                    }
                }
            });
            fileControl.setFileCategory(category.toString());
            poList.add(fileControl);
        });
        this.saveOrUpdateBatch(poList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeBatch(Long[] ids) {
        removeByIds(Arrays.asList(ids));
    }
}